<template>
    <el-drawer :size="dialogWidth" :title="configureTitle" v-model="configureOpen" append-to-body
               :modal="false" @close="close">
        <div align="center">
            <el-button type="primary" @click="addModelAttribute()">新增属性行</el-button>
            <el-button type="warning" @click="delModelAttribute()"
                       v-show="this.items.length!=1">删除属性行
            </el-button>
        </div>
        <div style="margin-left: 20px">
            <el-form ref="form" :inline="true" :rules="dataRules" :model="item" v-for="item in items">
                <el-form-item label="输入中文属性名：">
                    <el-input v-model="item.attributeNameChn"></el-input>
                </el-form-item>
                <el-form-item label="输入属性名：">
                    <el-input v-model="item.attributeName"></el-input>
                </el-form-item>
                <el-form-item label="输入属性类型：">
                    <el-select v-model="item.attributeType" style="height: auto;">
                        <el-option v-for="(dataType) in dataTypeOptions" :value="dataType.dictValue">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </div>
        <div align="center">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-drawer>
</template>

<script>
    import {addAgreementAttributes} from "/src/api/property/model";

    export default {
        name: "configure",
        emits: ['configureStatusChanged'],
        props: {
            //模型ID
            modelId: undefined,
            //标题
            configureTitle: String,
            //显隐
            configureOpen: Boolean,
            //属性列表
            items: Array,
        },
        data() {
            return {
                dataTypeOptions:[],
                dialogWidth: '60%',
                dataRules: {
                    attributeName: [
                        {required: true, message: "属性名不能为空", trigger: "blur"}
                    ],
                    attributeType: [
                        {required: true, message: "属性类型不能为空", trigger: "blur"}
                    ],
                    attributeNameChn: [
                        {required: true, message: "中文属性名不能为空", trigger: "blur"}
                    ],
                }
            }
        },
        created() {
            this.getDicts("sys_data_type").then(response => {
                this.dataTypeOptions = response.data;
            });
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        addAgreementAttributes(this.items).then(response => {
                            this.msgSuccess(this.configureTitle + "成功");
                            this.close();
                        });
                    }
                })
            },
            addModelAttribute() {
                const modelAttribute = {
                    attributeName: '',
                    attributeType: '',
                    attributeNameChn: '',
                    modelId: this.modelId,
                };
                this.items.push(modelAttribute);
            },
            delModelAttribute() {
                this.items.pop()
            },
            //取消
            cancel() {
                this.$emit('configureStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('configureStatusChanged', false)
            }
        },

    }
</script>

<style scoped>

</style>
